package com.charley.icloud.admin.webapps.sysrole.service.impl;

import com.charley.icloud.admin.common.constant.SysTableConstants;
import com.charley.icloud.admin.webapps.sysmenu.dao.SysMenuMapper;
import com.charley.icloud.admin.webapps.sysmenu.pojo.entity.SysMenuDO;
import com.charley.icloud.admin.webapps.sysrole.dao.SysRoleMapper;
import com.charley.icloud.admin.webapps.sysrole.dao.SysRoleMenuMapper;
import com.charley.icloud.admin.webapps.sysrole.pojo.entity.SysRoleDO;
import com.charley.icloud.admin.webapps.sysrole.pojo.entity.SysRoleMenu;
import com.charley.icloud.admin.webapps.sysrole.service.SysRoleService;
import com.charley.icloud.common.constants.SystemNumberConstants;
import com.charley.icloud.datasource.page.MybatisPageHelper;
import com.charley.icloud.datasource.page.PageRequest;
import com.charley.icloud.datasource.page.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public int save(SysRoleDO record) {
        if (record.getId() == null || record.getId().equals(SystemNumberConstants.SYSTEM_NUMBER_LONG_ZERO)) {
            Character code = 'A';
            // 处理code
            switch (record.getType()) {
                case (byte) 0:
                    code = 'A';
                    break;
                case (byte) 1:
                    code = 'B';
                    break;
                case (byte) 2:
                    code = 'C';
                    break;
                case (byte) 3:
                    code = 'D';
                    break;
                case (byte) 4:
                    code = 'E';
                    break;
                default:
                    code = 'F'; // 其他类型
            }
            int num = sysRoleMapper.findRoleNumbersByCode(code);
            record.setCode(code + String.format("%06d", num));
            record.setCreateBy("System");
            record.setCreateTime(new Date());
            return sysRoleMapper.insertSelective(record);
        }
        record.setLastUpdateBy("System");
        record.setLastUpdateTime(new Date());
        return sysRoleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(SysRoleDO record) {
        return sysRoleMapper.deleteByPrimaryKey(record);
    }

    @Override
    public int delete(List<SysRoleDO> records) {
        for (SysRoleDO record : records) {
            record.setLastUpdateBy("System");
            record.setLastUpdateTime(new Date());
            delete(record);
        }
        return 1;
    }

    @Override
    public SysRoleDO findById(Long id) {
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        Object label = pageRequest.getParam("name");
        if (label != null) {
            return MybatisPageHelper.findPage(pageRequest, sysRoleMapper, "findPageByName", label);
        }
        return MybatisPageHelper.findPage(pageRequest, sysRoleMapper);
    }

    @Override
    public List<SysRoleDO> findAll() {
        return sysRoleMapper.findAll();
    }

    public SysRoleMapper getSysRoleMapper() {
        return sysRoleMapper;
    }

    public void setSysRoleMapper(SysRoleMapper sysRoleMapper) {
        this.sysRoleMapper = sysRoleMapper;
    }

    @Override
    public List<SysMenuDO> findRoleMenus(Long roleId) {
        SysRoleDO sysRole = sysRoleMapper.selectByPrimaryKey(roleId);
        if (SysTableConstants.SYS_USER_TABLE_TYPE_SUPER_ADMIN.equals(sysRole.getType())) {
            // 如果是超级管理员，返回全部
            return sysMenuMapper.findAll();
        }
        return sysMenuMapper.findRoleMenus(roleId);
    }

    @Transactional
    @Override
    public int saveRoleMenus(List<SysRoleMenu> records) {
        for (SysRoleMenu record : records) {
            sysRoleMenuMapper.insertSelective(record);
        }
        return 1;
    }

    @Override
    public List<SysRoleDO> findByName(String name) {
        return sysRoleMapper.findByName(name);
    }

}
